<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="queryParams.dataType" placeholder="请选择数据类型" clearable size="small">
          <el-option v-for="item in dataTypeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable size="small">
          <el-option v-for="item in deviceTypeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产线名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入产线名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工位名称" prop="postName">
        <el-input v-model="queryParams.postName" placeholder="请输入工位名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="事件类型" prop="eventType">
        <el-select v-model="queryParams.eventType" placeholder="请选择事件类型" clearable size="small">
          <el-option v-for="item in eventTypeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作员名称" prop="operatorName">
        <el-input v-model="queryParams.operatorName" placeholder="请输入操作员名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="操作员工号" prop="operatorIdCard">
        <el-input v-model="queryParams.operatorIdCard" placeholder="请输入操作员工号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="记录状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择记录状态" clearable size="small">
          <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['lamp:event-record:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['lamp:event-record:export']">导出</el-button>
      </el-col>
          <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['lamp:event-record:delete']"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

            <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
      <el-table-column label="数据类型" align="center" prop="dataType">
        <template v-slot="scope">
          {{ getLabelByValue(dataTypeList, scope.row.dataType) }}
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template v-slot="scope">
          {{ getLabelByValue(deviceTypeList, scope.row.deviceType) }}
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="产线名称" align="center" prop="deptName" />
      <el-table-column label="工位名称" align="center" prop="postName" />
      <el-table-column label="事件类型" align="center" prop="eventTypeName" />
      <el-table-column label="事件名称" align="center" prop="eventName" />
      <el-table-column label="操作员名称" align="center" prop="operatorName" />
      <el-table-column label="操作员工号" align="center" prop="operatorIdCard" />
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.operationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录状态" align="center" prop="status">
        <template v-slot="scope">
          {{ getLabelByValue(statusList, scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['lamp:event-record:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['lamp:event-record:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <EventRecordForm ref="formRef" @success="getList" />
    </div>
</template>
<script>
import * as EventRecordApi from '@/api/lamp/eventrecord/index.js';
import { getDictDatas, DICT_TYPE } from '@/utils/dict'
import EventRecordForm from './EventRecordForm.vue';
export default {
  name: "EventRecord",
  components: {
          EventRecordForm,
  },
  data() {
    return {
      deviceTypeList: getDictDatas(DICT_TYPE.DEVICE_TYPE),
      // 数据类型数组
      dataTypeList: [
        { label: '正常生产', value: 1 },
        { label: '返工', value: 2 },
        { label: '试产', value: 3 }
      ],
      // 事件类型数组
      eventTypeList: [
        { label: '下线', value: 0 },
        { label: '上线', value: 1 },
        { label: '复位', value: 2 },
        { label: '缺人', value: 3 },
        { label: '缺料', value: 4 },
        { label: '其他', value: 5 },
        { label: '换线', value: 6 },
        { label: '品质异常', value: 7 },
        { label: '设备&测试异常', value: 8 },
        { label: '取消', value: 9 }
      ],
      // 记录状态数组
      statusList: [
        { label: '有效', value: 1 },
        { label: '无效', value: 0 }
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
              // 总条数
      total: 0,
      // 上报事件列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      checkedIds: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        dataType: null,
        deviceType: null,
        deviceCode: null,
        deviceName: null,
        deptId: null,
        deptName: null,
        postId: null,
        postName: null,
        eventType: null,
        eventName: null,
        operatorId: null,
        operatorName: null,
        operatorIdCard: null,
        operationTime: [],
        status: null,
        createTime: [],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getLabelByValue(list, value) {
      const item = list.find(item => item.value === value);
      return item ? item.label : value;
    },
    async getList() {
      try {
      this.loading = true;
              const res = await EventRecordApi.getEventRecordPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除上报事件编号为"' + id + '"的数据项?')
      try {
       await EventRecordApi.deleteEventRecord(id);
       await this.getList();
       this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 批量删除上报事件 */
    async handleDeleteBatch() {
      await this.$modal.confirm('是否确认删除?')
      try {
        await EventRecordApi.deleteEventRecordList(this.checkedIds);
        this.checkedIds = [];
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    handleRowCheckboxChange(records) {
      this.checkedIds = records.map((item) => item.id);
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有上报事件数据项?');
      try {
        this.exportLoading = true;
        const data = await EventRecordApi.exportEventRecordExcel(this.queryParams);
        this.$download.excel(data, '上报事件.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
              }
};
</script>
